package com.cr.admin.business.topic.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.cr.admin.business.topic.pojo.entity.Topic;
import com.cr.admin.business.topic.pojo.param.TopicAddParam;
import com.cr.admin.business.topic.pojo.param.TopicUpdateParam;
import com.cr.admin.business.topic.pojo.query.TopicQuery;
import com.cr.admin.business.topic.service.TopicService;
import com.cr.common.response.JsonResult;
import com.cr.common.response.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

@Api(tags = "80.专题管理")
@RequestMapping("/api/bus/topics")
public class TopicController {
    @Autowired
    private TopicService topicService;

    @SaCheckPermission("bus:content:topic:query")
    @ApiOperation(value = "查询专题列表")
    @GetMapping
    public JsonResult list(TopicQuery topicQuery) {
        PageData<Topic> pageData = PageData.page(topicQuery.getPageNum(), topicQuery.getPageSize(), () -> topicService.list(topicQuery));
        return JsonResult.success(pageData);
    }

    @SaCheckPermission("bus:content:topic:add")
    @ApiOperation(value = "新增专题")
    @PostMapping
    public JsonResult insert(@Validated @RequestBody TopicAddParam topicAddParam) {
        topicService.insert(topicAddParam);
        return JsonResult.success();
    }

    @SaCheckPermission("bus:content:topic:edit")
    @ApiOperation(value = "修改专题")
    @PutMapping()
    public JsonResult update(@Validated @RequestBody TopicUpdateParam topicUpdateParam) {
        topicService.updateById(topicUpdateParam);
        return JsonResult.success();
    }
    @SaCheckPermission("bus:content:topic:query")
    @ApiOperation(value = "查询专题详情")
    @GetMapping("{id}")
    public JsonResult getDetail(@PathVariable("id") Long id) {
        Topic topic = topicService.selectById(id);
        return JsonResult.success(topic);
    }
    @SaCheckPermission("bus:content:topic:del")
    @ApiOperation(value = "删除专题")
    @DeleteMapping()
    public JsonResult delete(@RequestParam(name = "ids") List<Long> ids){
        topicService.deleteByIds(ids);
        return JsonResult.success();
    }
    @SaCheckPermission("bus:content:topic:query")
    @ApiOperation(value = "查询全部专题")
    @GetMapping("/all")
    public JsonResult all() {
        List<Topic> list = topicService.selectList(null);
        return JsonResult.success(list);
    }

}
